/*
 * Copyright 2011-2012 Benoit Averty, Samuel Babin, Matthieu Bergere, Thomas Letan, Sacha Percot-Tétu, Florian Teyssier
 * 
 * This file is part of DETIQ-T.
 * 
 * DETIQ-T is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * DETIQ-T is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with DETIQ-T.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef IMAGEWINDOW_H
#define IMAGEWINDOW_H


#include <QWidget>
#include <QLabel>
#include <QString>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QScrollArea>
#include <QFont>
#include <QKeyEvent>
#include <QPoint>
#include <QInputDialog>
#include <QToolButton>
#include <QStatusBar>
#include <QDrag>

#include <sstream>
#include <iostream>
#include <list>

#include <Rectangle.h>

#include <Image.h>
#include "ImageView.h"
#include "D:/Users/flenorma/Mes documents/EIIMAGE/officielle/eiimage_project/detiq-t/GenericInterface/Widgets/ImageWidgets/ImageContextMenu.h"
#include "../../Other/ParamRep.h"
#include "../../eiimage/core/Operation.h"

class QSpinBox;

namespace genericinterface
{

class GenericInterface;

class SelectionWidget : public QWidget {
  Q_OBJECT
  public:
    SelectionWidget(QWidget* parent, int width, int height);
    void setRange(int width, int height);
  public slots:
    void updateSelection(QRect select);
    void selectionMoved(int);
  signals:
    void selectionMoved(QRect);
  protected:
    QSpinBox *_Xspinbox, *_Yspinbox, *_Wspinbox, *_Hspinbox;
};
/*!
   * \brief Inherited class by all Windows
   *
   * The ImageWindow class contains the status bar and inherit QWidget.
   */
class ImageWindow : public QWidget
{
    Q_OBJECT
    public:
        /*!
         * \brief Default constructor.
         *
         * Constructs an empty QWidget and initialized the source window and the application area from parameters.
         *
         * \param source The source window
         * \param rect The application area (Rectangle used for Histogram, Algorithm...)
         */
        ImageWindow(QString path, Image *image = NULL, imagein::Rectangle rect = imagein::Rectangle(), bool normalize = false, bool logScale = false, double logConstantScale = 1.0, bool abs = false);

        ~ImageWindow();

        /*!
         * \brief Returns the title from a path.
         *
         * \param path The path of a file.
         */
        static QString getTitleFromPath(QString path);

        inline QString getPath() const { return _path; }
        inline void setPath(QString path) { _path = path; }

        inline QString getName() const { return getTitleFromPath(_path); }

        /*!
         * \brief Returns the displayable Image contained in the window.
         *
         */
        inline const imagein::Image* getDisplayImage() const { return _displayImg; }

        /*!
         * \brief Returns the Rectangle which is the current image's selection on the window.
         *
         */
        const imagein::Rectangle selection() const { return _imageView->getRectangle(); }

        inline QMenu* menu() { return _menu; }
        inline QPoint selectedPixel() const { return _selectedPixel; }

        inline ImageView* view() { return _imageView; }

        void zoom(int delta);
        virtual bool isDouble() const = 0;
        virtual bool isStandard() const = 0;

        enum Mode{USER,CONS,REST};

        virtual Image* makeDispImage(Mode mode)=0;

        void * setDisplayImage(Mode mode = ImageWindow::CONS);
        QString getExtension(){return _extension;}
        QString getName(){return _name;}

        QLabel *processingmade;





    public slots:
        void activated();
        void setApplicationArea(const imagein::Rectangle rect);
        void startDrag();
        void updateZoom(double z) const;
        void toggleMouseMode(bool);
        void toggleSelectMode(bool);

        virtual void crop() = 0;
        virtual void copycrop() = 0;

        virtual void pixelClicked(int x, int y);
        virtual void pixelHovered(int x, int y);

        void rename();
        void applyBinaryMask();

        void setParam(ParamRep *param);
        void applyParam(ParamRep *param);





    signals:
        /*!
         * \brief Signal emits when this is activated
         *
         * \param rect Rectangle to display on the source window
         */
        void selectRectChange(imagein::Rectangle rect, ImageWindow* source);

        /*!
         * \brief Signal emits when the highlight rect have to changes
         *
         * \param rect The rectangle to display
         */
        void highlightRectChange(imagein::Rectangle rect, ImageWindow* source);

        void addWidget(ImageWindow*, QWidget*);
        void addImage(ImageWindow*, ImageWindow*);
        void applyBinaryMask(ImageWindow*);
        void imageChanged(ImageWindow*);


    protected:
        void changeRepresentation( bool normalize, bool logScale, double logConstantScale, bool abs);

        virtual void setMemoryImage();


        int _maxtotal;
        int _mintotal;
        int _valuemin;
        int _valuemax;
        int _valuecenter;

        ParamRep *_param;

        imagein::Rectangle _applicationArea;
        QString _path;

        //display of the image
        imagein::Image* _displayImg;
        ImageView* _imageView;
        //parameter
        bool _normalize;
        bool _logScale;
        double _logConstantScale;
        bool _abs;

        ImageContextMenu* _menu;

        QWidget* _statusBar;
        QLabel* _lImageSize;
        QLabel* _lImageName;
        QLabel* _lZoom;
        QPoint _selectedPixel;
        QToolButton* _selectButton;
        QToolButton* _mouseButton;
        QToolButton* _selectAllButton;
        SelectionWidget* _selectWidget;
        QWidget* _infoWidget;
        QVBoxLayout* _infoLayout;

        void initStatusBar();
        void updateStatusBar();
        virtual void updateSpecStatusBar()=0;

        double _zoomFactor;

        void wheelEvent (QWheelEvent * event);
        void keyPressEvent ( QKeyEvent * event );


        virtual void showHoveredPixelInformations(int x, int y) const = 0;
        virtual void showSelectedPixelInformations(int x, int y) const = 0;

        QLabel* _lStats;

        QLabel* _lHoveredPixelInfo;
        QLabel* _lHoveredPixelPosition;
        QLabel* _lHoveredPixelColor;
        QLabel* _lHoveredPixelRGBColor;
        QLabel* _lHoveredPixelHSLColor;
        QLabel* _lHoveredPixelHSVColor;

        QLabel* _lSelectedPixelInfo;
        QLabel* _lSelectedPixelPosition;
        QLabel* _lSelectedPixelColor;
        QLabel* _lSelectedPixelRGBColor;
        QLabel* _lSelectedPixelHSLColor;
        QLabel* _lSelectedPixelHSVColor;

        QString _extension;
        QString _name;
        QLabel *typeoftheimage;


};

}

#endif // IMAGEWINDOW_H
